﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PayrollApp.Forms;

namespace PayrollApp.MainForm
{
    public partial class MDIParentForm : Form
    {
        private int childFormNumber = 0;
        public Form CurrentActiveForm;        
        public MDIParentForm()
        {
            InitializeComponent();
        }

#region FUNCTIONS
    
    public void loadChildform(Form _form)
    {
        CurrentActiveForm = _form;
        _form.MdiParent = this;
        _form.Show();
        _form.StartPosition = FormStartPosition.Manual;
        _form.Top = 0;
        _form.Left = 0;
        _form.Activate();
        _form.Focus();    
    }
    private void closeChildren()
    {
        foreach (Form childForm in MdiChildren)
        {
            childForm.Close();
        }
    }
    private void closeForm(Form _form)
    {
        foreach (Form childForm in MdiChildren)
        {
            if (childForm == _form)
            {
                childForm.Close();
                return;
            }               
        }
    }
    //  Check if form is loaded
    private bool isFormActive(Form _form) 
    {
        return _form != null && _form.IsMdiChild;
    }
#endregion

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void tbrNew_Click(object sender, EventArgs e)
        {

        }

        private void tbrRefresh_Click(object sender, EventArgs e)
        {            
        }

        private void tbrClose_Click(object sender, EventArgs e)
        {
            Application.Exit();            
        }

        private void tbrEdit_Click(object sender, EventArgs e)
        {            
            if (!isFormActive(CurrentActiveForm))
            {
                EmployeeAddForm empForm = new EmployeeAddForm(SaveMode.Add);                
                loadChildform(empForm);            
            }
        }

        private void MDIParentForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();      
        }

        private void tbrNew_Click_1(object sender, EventArgs e)
        {
            if (!isFormActive(CurrentActiveForm))
            {
                UsersListing usersListing = new UsersListing();
                loadChildform(usersListing);
            }
        }

    }
}
